import com.google.common.base.Preconditions;
import org.aceor.mddal.client.jdbc.DataSourceFacade;
import org.aceor.mddal.group.DataSourceWrapper;
import org.aceor.mddal.group.GroupDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.jdbc.core.JdbcTemplate;

import java.sql.*;

/**
 * Created by lxue on 16/3/20.
 */
public class Main {
    public static void main(String[] args) throws SQLException, ClassNotFoundException {
//        System.out.println(SequenceUtil.generateUniqueSequenceNum());
        ApplicationContext applicationContext = new ClassPathXmlApplicationContext("spring/applicationContext.xml");

        JdbcTemplate jdbcTemplate = applicationContext.getBean("jdbcTemplate", JdbcTemplate.class);
        if (true) {
            return;
        }
        Preconditions.checkNotNull(((DataSourceFacade)jdbcTemplate.getDataSource()).getDsConfigPath());
//        Integer countAll = jdbcTemplate.queryForObject("SELECT COUNT(*) FROM t_addr", Integer.class);
//        System.out.println(countAll);

        DataSourceFacade dataSourceFacade = applicationContext.getBean("myDataSource", DataSourceFacade.class);

        System.out.println(dataSourceFacade.getDsMapHolder().get());

        GroupDataSource dataSource = (GroupDataSource) dataSourceFacade.getDsMapHolder().get().get("MDDAL_APP_0_GROUP");
        System.out.println(dataSource.getDataSourceWrapperMap());

        DataSourceWrapper dataSourceWrapper =  dataSource.getDataSourceWrapperMap().get("MDDAL_APP_1_ATOM");
        Connection connection = dataSourceWrapper.getWrappedDataSource().getConnection();
        System.out.println(connection);
        connection.setAutoCommit(false);
        Statement statement = connection.createStatement();
        ResultSet rs = statement.executeQuery("SELECT COUNT(*) from t_stu");
        System.out.println(rs);
        while (rs.next()) {
            System.out.println(rs.getInt(1));
        }
//        connection.commit();
//        rs.close();
        PreparedStatement ps = connection.prepareStatement("update t_stu set name='ddd' where id = 2");
        System.out.println(ps.executeUpdate());
//        ResultSet resultSet = ps.executeQuery();
//        while (resultSet.next()) {
//            System.out.println(resultSet.getObject(2));
//        }
        connection.commit();
//        connection.close();
//        statement.close();
//        DriverManager.getDriver("jdbc:mysql://localhost:3306/mddal_test_db_1");
        System.out.println(123);

    }
}
